package com.shenhaofeng.app.component;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.shenhaofeng.app.camera.Camera;
import com.shenhaofeng.app.camera.CameraController;
import com.shenhaofeng.app.camera.Option;
import com.shenhaofeng.app.component.fragment.LazyController;
import com.trello.rxlifecycle.components.support.RxFragment;


/**
 * fragment基类
 * Created by 沈豪烽 on 2016/8/29.
 */
public class BaseFragment extends RxFragment implements OnTransactionHandler,LazyController.Callback {


    private CameraController mCameraController;


    private FragmentTransactionCompat mFragmentTransactionCompat;

    private LazyController mLazyController;






    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLazyController=new LazyController(this);
        mCameraController = CameraController.create(new CameraCallback());
        mCameraController.onCreate(savedInstanceState);
        mFragmentTransactionCompat = new FragmentTransactionCompat(this, savedInstanceState);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLazyController.setPrepared(true);
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mCameraController.onSaveInstanceState(outState);
        mFragmentTransactionCompat.onFragmentSaveInstanceStateAfter(outState);
    }


    @CallSuper
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCameraController.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mLazyController.setPrepared(false);
    }


    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onResume() {
        super.onResume();
        mFragmentTransactionCompat.onResumeFragment();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(mLazyController!=null){
            mLazyController.setUserVisibleHint(isVisibleToUser);
        }
    }




    /**
     * 打开相机
     *
     * @param request
     * @param option
     */
    public void openCamera(int request, Option option) {
        mCameraController.openCamera(this, request, option);
    }


    /**
     * 打开相册
     *
     * @param request
     */
    public void openAlbum(int request) {
        mCameraController.openAlbum(this, request);
    }


    public BaseFragment getFragment() {
        return this;
    }


    public CameraController getCameraController() {
        return mCameraController;
    }




    public FragmentTransactionCompat getFragmentTransactionCompat() {
        return mFragmentTransactionCompat;
    }


    /**
     * 跳转至目标Activity
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void toActivity(Class<? extends Activity> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getContext(), cls);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }


    /**
     * 跳转至目标Activity
     *
     * @param cls
     * @param bundle
     */
    public void toActivity(Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent(getContext(), cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    /**
     * 跳转至目标Activity
     *
     * @param cls
     */
    public void toActivity(Class<? extends Activity> cls) {
        toActivity(cls, null);
    }


    /**
     * 跳转至目标Activity
     *
     * @param cls
     * @param requestCode
     */
    public void toActivity(Class<? extends Activity> cls, int requestCode) {
        toActivity(cls, null, requestCode);
    }


    /**
     * 重写该方法处理拍照的回调
     *
     * @param request
     * @param result
     * @param camera
     */
    protected boolean onCameraResponse(int request, boolean result, Camera camera) {
        return false;
    }



    /**
     * 重写该方法处理相册的回调
     *
     * @param request
     * @param result
     * @param data
     * @return
     */
    protected boolean onAlbumResponse(int request, boolean result, Uri data) {
        return false;
    }


    @Override
    public void handleFragmentDelayMessage(FragmentTransactionCompat.DelayMessage message) {

    }


    @Override
    public void onLazyLoad(boolean isFirst) {
    }


    class CameraCallback implements CameraController.Callback {

        @Override
        public boolean onCamera(int request, boolean result, Camera camera) {
            return onCameraResponse(request, result, camera);
        }


        @Override
        public boolean onSelectedPhoto(int request, boolean result, Uri uri) {
            return onAlbumResponse(request, result, uri);
        }


    }


}
